<script setup lang="tsx">
import { activeListApi } from '@/api/Activity'
import { businessListApi } from '@/api/merchantManagement'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
// import { useI18n } from '@/hooks/web/useI18n'
import { useTable } from '@/hooks/web/useTable'
import { ActiveStateDict } from '@/utils/dict'
import { hasPermission as hasPermis } from '@/components/Permission/src/utils'

// const { t } = useI18n()

const activeTab = ref(0)

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const res = await activeListApi({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      activeType: 3,
      tab: 'EXTERNAL',
      activeState: unref(activeTab) === 0 ? '' : unref(activeTab),
      ...unref(searchParams)
    })
    return {
      list: res.data.list || [],
      total: res.data.total || 0
    }
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList } = tableMethods

const tabs = [
  {
    label: '全部',
    value: 0
  },
  {
    label: '进行中',
    value: 3
  },
  {
    label: '未开始',
    value: 2
  },
  {
    label: '已结束',
    value: 4
  }
]

const searchRef = ref()

const tabChange = (v) => {
  searchRef.value[0].reset()
  activeTab.value = v
  currentPage.value = 1
  searchParams.value = {}
  getList()
}

const getBusinessList = async () => {
  if (!hasPermis('GET /business/page')) {
    allSchemas.searchSchema.at(1)!.hidden = true
    return []
  }
  const res = await businessListApi({
    pageNum: 1,
    pageSize: 9999
  })
  return res.data.list?.map((v) => ({
    label: v.businessShortName,
    value: v.id
  }))
}

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: 'ID',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      hidden: true
    }
  },
  {
    field: 'activeNo',
    label: '活动编号',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      width: 220
    }
  },
  {
    field: 'activeTitle',
    label: '活动名称',
    search: {
      componentProps: {
        placeholder: '请输入活动名称'
      }
    },
    form: {
      hidden: true
    },
    table: {
      width: 220
    }
  },
  {
    field: 'activePrice',
    label: '活动价格',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'originPrice',
    label: '划线价格',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'activeUrl',
    label: '活动链接',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'businessName',
    label: '活动商家',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'businessId',
    label: '活动商家',
    search: {
      component: 'Select',
      optionApi: async () => getBusinessList()
    },
    table: {
      hidden: true
    }
  },
  {
    field: 'activeState',
    label: '状态',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <span>{row.activeState && ActiveStateDict[row.activeState]}</span>
            </>
          )
        }
      }
    }
  },
  {
    field: 'time',
    label: '活动时间',
    search: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetimerange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    form: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <span>{row.startTime}</span>至<span>{row.endTime}</span>
            </>
          )
        }
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const searchParams = ref({})
const setSearchParams = (params: any) => {
  const { time, ...rest } = params
  let startTime, endTime
  if (time && time.length > 0) {
    startTime = time[0]
    endTime = time[1]
  }
  currentPage.value = 1
  searchParams.value = {
    startTime,
    endTime,
    ...rest
  }
  getList()
}
</script>

<template>
  <div class="flex w-100% h-100%">
    <ContentWrap class="flex-[3] ml-20px">
      <ElTabs v-model="activeTab" @tab-change="tabChange">
        <ElTabPane v-for="item in tabs" :key="item.value" :label="item.label" :name="item.value">
          <Search
            ref="searchRef"
            :schema="allSchemas.searchSchema"
            @reset="setSearchParams"
            @search="setSearchParams"
          />
          <Table
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :columns="allSchemas.tableColumns"
            :data="dataList"
            :loading="loading"
            @register="tableRegister"
            :pagination="{
              total
            }"
          />
        </ElTabPane>
      </ElTabs>
    </ContentWrap>
  </div>
</template>
